//== SAGITTARIUS ============================================================================

//== BEGINNING OF CODE ===============================================================

#include "sagittarius/core/SagittariusCore.h"

//SAGITTARIUS headers:
#include "sagittarius/unicode/CUnicode.h"
#include "sagittarius/re/reParser.h"
#include "sagittarius/re/reDeriv.h"

#include <stdio.h>
//#include <time.h>
//#include <malloc.h>
//#include <string.h>

expNode* re_computeDerivative(expNode* n, const UnicodeCChar c) {
    //TODO: these deep copies may not be necessary. Try it without them.
    //Will need a node manager in that case, as some nodes may be orphaned
    AN(n);
    #define r (n->n1)
    #define s (n->n2)
    expNode* rp;
    expNode* sp;
    
    if(n->closed)
        re_constructNodeConcat(re_computeDerivative(r, c), re_deepCopy(n))
    switch(n->type) {
        case AST_NODE_TYPE_CONCAT:
            if(r->nullable)
                return re_constructNodeUnionN(
                    re_constructNodeConcatN(re_computeDerivative(r, c), re_deepCopy(s)), re_computeDerivative(s, c) );
            else
                return re_constructNodeConcatN(re_computeDerivative(r, c), re_deepCopy(s));
        case AST_NODE_TYPE_UNION:
            return re_constructNodeUnionN(re_computeDerivative(r, c), re_computeDerivative(s, c));
        case AST_NODE_TYPE_INTERSECTION:
            return re_constructNodeIntN(re_computeDerivative(r, c), re_computeDerivative(s, c));
    #undef r
    #undef s
}
